﻿using Grow.Domain.EventBus;

namespace Grow.Domain.Object
{
    /// <summary>
    /// 聚合根
    /// </summary>
    public abstract class AggregateRootBase : EntityBase, IAggregateRoot
    {
        private readonly List<IDomainEvent> _domainEvents;
        public AggregateRootBase(Guid id) : base(id)
        {
            _domainEvents = new();
        }

        public virtual void AddDomainEvent(IDomainEvent domainEvent)
        {
            _domainEvents.Add(domainEvent);
        }

        public virtual void RemoveDomainEvent(IDomainEvent domainEvent)
        {
            _ = _domainEvents.Remove(domainEvent);
        }

        public virtual IEnumerable<IDomainEvent> GetDomainEvents()
        {
            return _domainEvents;
        }

        public void ClearDomainEvents()
        {
            _domainEvents.Clear();
        }
    }
}
